package controller;

import Service.UserService;
import Service.impl.UserServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.8.28 下午 15:24
 */
@javax.servlet.annotation.WebServlet("/login")
public class LoginServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//解决post提交的中文乱码
        response.setContentType("text/html;charset= utf-8");//设置响应乱码

        //接收前台的数据
        String name = request.getParameter("name");
        String password = request.getParameter("password");

        //创建Service层接口对象
        UserService userService = new UserServiceimpl();

        boolean flag = userService.getUser(name, password);


        //进行判断
        if (flag) {
            request.getRequestDispatcher("/findAllpro").forward(request, response);
        } else {
            request.setAttribute("msg", "用户名或密码输入有误");
            //请求转发到login.jsp
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);

    }
}
